unit uDemoInit;

interface
uses
  Forms,
  GlScene, GlObjects, GLCadencer, GLWin32Viewer, GlMaterial;
//------------------------------------------------------------------------------
  Procedure InitDemo(const aGlScene: TGLScene; var aGlCamera: TGLCamera; var aMainDummy: TGlDummyCube; var aRenderer: TGLDirectOpenGL);
  Procedure CreateDemo(const aForm: TForm; var aGlScene: TGlScene; var aCadencer: TGLCadencer; var aViewer: TGLSceneViewer);
//------------------------------------------------------------------------------
  Function AddSimpleMaterial(aMatLib: TGlMaterialLibrary; aMaterialName: String; aR, aG, aB, aAlpha: byte): TGlLibMaterial;
  Function AddMaterial(aMatLib: TGlMaterialLibrary; aFileName, aMaterialName: String): TGlLibMaterial;
  Procedure AddMaterials(aMatLib: TGlMaterialLibrary; aFolder: String; aFiles: array of String; aNames: array of String);
//------------------------------------------------------------------------------
implementation
uses
  Controls, Graphics, GlTexture, GlCompositeImage, GlFilePNG, GlFileJPeG,
  GLRenderContextInfo;
//------------------------------------------------------------------------------
Procedure AddMaterials(aMatLib: TGlMaterialLibrary; aFolder: String; aFiles: array of String; aNames: array of String);
var
  i: integer;
begin
  for i := low(aFiles) to high(aFiles) do
    if (i >= low(aNames)) and (i <= high(aNames)) then
      AddMaterial(aMatLib, aFolder + aFiles[i], aNames[i]);
end;
//------------------------------------------------------------------------------
Function AddSimpleMaterial(aMatLib: TGlMaterialLibrary; aMaterialName: String; aR, aG, aB, aAlpha: byte): TGlLibMaterial;
begin
  result := aMatLib.Materials.Add;
  with result do
  begin
    with Material do
    begin
      MaterialOptions := [moIgnoreFog, moNoLighting];
      Texture.Disabled := true;
      with FrontProperties do
      begin
        Ambient. SetColor(aR/255, aG/255, aB/255, aAlpha/255);
        Diffuse. SetColor(aR/255, aG/255, aB/255, aAlpha/255);
        Emission.SetColor(aR/255, aG/255, aB/255, aAlpha/255);
        Specular.SetColor(aR/255, aG/255, aB/255, aAlpha/255);
      end;
    end;
    Name := aMaterialName;
  end;
end;
//------------------------------------------------------------------------------
Function AddMaterial(aMatLib: TGlMaterialLibrary; aFileName, aMaterialName: String): TGlLibMaterial;
begin
  result := aMatLib.Materials.Add;
  with result do
  begin
    with Material do
    begin
      MaterialOptions := [moIgnoreFog, moNoLighting];
      Texture.Disabled := false;
      BlendingMode := bmTransparency;
      Texture.TextureMode := tmModulate;
      with FrontProperties do
      begin
        Ambient. SetColor(1,1,1, 1);
        Diffuse. SetColor(1,1,1, 1);
        Emission.SetColor(1,1,1, 1);
        Specular.SetColor(1,1,1, 1);
      end;
      Texture.ImageClassName := 'TGLCompositeImage';
      TGLCompositeImage(Texture.Image).LoadFromFile(aFileName);
      //Texture.AllocateHandle;
    end;
    Name := aMaterialName;
  end;
end;
//------------------------------------------------------------------------------
Procedure InitDemo(const aGlScene: TGLScene; var aGlCamera: TGLCamera; var aMainDummy: TGlDummyCube; var aRenderer: TGLDirectOpenGL);
begin
  aGlCamera := TGlCamera.CreateAsChild(aGlScene.Objects);
  with aGlCamera do
  begin
    Position.SetPoint(0, 0, 5);
    Direction.SetVector(0, 0, -1);
    Up.SetVector(0, 1, 0);
    CameraStyle := csOrthogonal;
    SceneScale := 0.1;
  end;

  aMainDummy := TGLDummyCube.CreateAsChild(aGlScene.Objects);
  aMainDummy.ObjectsSorting := osNone;

  aRenderer := TGLDirectOpenGL.CreateAsChild(aMainDummy);
end;
//------------------------------------------------------------------------------
Function RGB(r, g, b: byte): TColor;
begin
  result := r + (g shl 8) + b shl 16;
end;
//------------------------------------------------------------------------------
Procedure CreateDemo(const aForm: TForm; var aGlScene: TGlScene; var aCadencer: TGLCadencer; var aViewer: TGLSceneViewer);
begin
  aGlScene := TGlScene.Create(aForm);

  aCadencer := TGLCadencer.Create(aForm);
  with aCadencer do
  begin
    Enabled := false;
    SleepLength := 5;
    Mode := cmApplicationIdle;
    Scene := aGlScene;
  end;

  aViewer := TGLSceneViewer.Create(aForm);
  aViewer.Buffer.BackgroundColor := RGB(60, 60, 60);
  with aViewer do
  begin
    Parent := aForm;
    aViewer.Align := alClient;
  end;   
end;
//------------------------------------------------------------------------------
end.
